﻿using System;
using System.Data;
using System.Collections.Generic;
using XF.Common;
using XF.Model;
using XF.DALFactory;
using XF.IDAL;
using System.Collections;
using System.Data.SqlClient;
using XF.DBUtility;
using Enums;

namespace XF.BLL
{
	/// <summary>
	/// Bill_DTOHead
	/// </summary>
	public partial class Bill_DTOHead
	{
		private readonly IBill_DTOHead dal=DataAccess.CreateBill_DTOHead();
		private readonly IBill_DTOItems dalItems = DataAccess.CreateBill_DTOItems();
		public Bill_DTOHead()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			return dal.Exists(Id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(XF.Model.Bill_DTOHead model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XF.Model.Bill_DTOHead model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			return dal.Delete(Id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			return dal.DeleteList(Idlist);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_DTOHead GetModel(int Id)
		{
			
			return dal.GetModel(Id);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_DTOHead GetModel(string orderCode)
		{
			return dal.GetModel(orderCode);
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<XF.Model.Bill_DTOHead> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<XF.Model.Bill_DTOHead> DataTableToList(DataTable dt)
		{
			List<XF.Model.Bill_DTOHead> modelList = new List<XF.Model.Bill_DTOHead>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				XF.Model.Bill_DTOHead model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
		//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod
		// <summary>
		/// 获取分页数据
		/// </summary>
		/// <param name="strWhere"></param>
		/// <param name="strOrder"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public DataTable GetListByPage(string strWhere, string strOrder, int pageIndex, int pageSize, ref int count)
        {
			return dal.GetListByPage(strWhere, strOrder, pageIndex, pageSize, ref count);
        }

		public DataTable GetDetailListByPage(string strWhere, string strOrder, int pageIndex, int pageSize, ref int count)
        {
			return dal.GetDetailListByPage(strWhere, strOrder, pageIndex, pageSize, ref count);
        }


		public DictionaryEntry GetAddSql(XF.Model.Bill_DTOHead model)
		{
			return dal.GetAddSql(model);
		}

		public DictionaryEntry GetUpdateSql(XF.Model.Bill_DTOHead model)
        {
			return dal.GetUpdateSql(model);
        }

		public int SaveOrder(XF.Model.Bill_DTOHead model)
		{
			Dictionary<string, SqlParameter[]> SQLStringList = new Dictionary<string, SqlParameter[]>();
			model.CreateDate = DateTime.Now;
			model.CreateUser = LoginInfo.LoginName;
			model.LastUpdateDate = DateTime.Now;
			model.LastUpdateUser = LoginInfo.LoginName;
			DictionaryEntry entry = GetAddSql(model);
			SQLStringList.Add(SQLStringList.Count.ToString().PadLeft(5, '0') + entry.Key.ToString(), (SqlParameter[])entry.Value);
			foreach (XF.Model.Bill_DTOItems item in model.Items)
			{
				item.CreateDate = DateTime.Now;
				item.CreateUser = LoginInfo.LoginName;
				item.LastUpdateDate = DateTime.Now;
				item.LastUpdateUser = LoginInfo.LoginName;
				entry = dalItems.GetAddSql(item);
				SQLStringList.Add(SQLStringList.Count.ToString().PadLeft(5, '0') + entry.Key.ToString(), (SqlParameter[])entry.Value);
			}
			//执行事务
			SqlServerHelper.ExecuteSqlTran(SQLStringList);
			return (int)SqlEnum.Success;
		}

		public int UpdateOrder(XF.Model.Bill_DTOHead model)
		{
			Dictionary<string, SqlParameter[]> SQLStringList = new Dictionary<string, SqlParameter[]>();
			model.LastUpdateDate = DateTime.Now;
			model.LastUpdateUser = LoginInfo.LoginName;
			DictionaryEntry entry = GetUpdateSql(model);
			SQLStringList.Add(SQLStringList.Count.ToString().PadLeft(5, '0') + entry.Key.ToString(), (SqlParameter[])entry.Value);
			entry = dalItems.GetDeleteSql(model.OrderCode);
			SQLStringList.Add(SQLStringList.Count.ToString().PadLeft(5, '0') + entry.Key.ToString(), (SqlParameter[])entry.Value);
			foreach (XF.Model.Bill_DTOItems item in model.Items)
			{
				item.LastUpdateDate = DateTime.Now;
				item.LastUpdateUser = LoginInfo.LoginName;
				entry = dalItems.GetAddSql(item);
				SQLStringList.Add(SQLStringList.Count.ToString().PadLeft(5, '0') + entry.Key.ToString(), (SqlParameter[])entry.Value);
			}
			//执行事务
			SqlServerHelper.ExecuteSqlTran(SQLStringList);
			return (int)SqlEnum.Success;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetDetailList(string strWhere)
		{
			return dal.GetDetailList(strWhere);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_DTOHead GetDetailModel(int Id)
		{
			return dal.GetDetailModel(Id);
		}

		/// <summary>
		/// 更新状态
		/// </summary>
		public bool UpdateStatus(string orderCode, int status)
        {
			return dal.UpdateStatus(orderCode, status);
        }

		/// <summary>
		/// 更新状态
		/// </summary>
		public bool Enable(string orderCode)
		{
			return dal.Enable(orderCode);
		}

		/// <summary>
		/// 更新状态
		/// </summary>
		public bool Disable(string orderCode)
        {
			return dal.Disable(orderCode);
        }

		/// <summary>
		/// 获取指定日期、用户的最大流水号
		/// </summary>
		/// <param name="mrp"></param>
		/// <param name="dateTime"></param>
		/// <returns></returns>
		public int GetMaxOrderCode(string mrp, DateTime dateTime)
        {
			return dal.GetMaxOrderCode(mrp, dateTime);
        }

		public string GetNewOrderCode(string mrp, DateTime dateTime)
		{
			int iNo = GetMaxOrderCode(mrp, dateTime);
			iNo++;
			return dateTime.ToString("yyyyMMdd") + iNo.ToString("D3") + mrp;
		}
		#endregion  ExtensionMethod
	}
}

